package com.cheer.ba.choosepic.adp;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.cheer.ba.R;
import com.cheer.ba.choosepic.util.BitmapCache;
import com.cheer.ba.choosepic.util.ImageItem;
import com.cheer.ba.choosepic.util.Res;

import java.util.ArrayList;

/**
 * 这个是显示一个文件夹里面的所有图片时用的适配器
 *
 * @author king
 * @version 2014年10月18日  下午11:49:35
 * @QQ:595163260
 */
public class AlbumGridViewAdp extends BaseAdapter {
    final String TAG = getClass().getSimpleName();
    private Context mContext;
    private ArrayList<ImageItem> dataList;
    private ArrayList<ImageItem> selectedDataList;
    private DisplayMetrics dm;
    BitmapCache cache;

    public AlbumGridViewAdp(Context c, ArrayList<ImageItem> dataList, ArrayList<ImageItem> selectedDataList) {
        mContext = c;
        cache = new BitmapCache();
        this.dataList = dataList;
        this.selectedDataList = selectedDataList;
        dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
    }

    public int getCount() {
        return dataList.size();
    }

    public Object getItem(int position) {
        return dataList.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    BitmapCache.ImageCallback callback = new BitmapCache.ImageCallback() {
        @Override
        public void imageLoad(ImageView imageView, Bitmap bitmap,
                              Object... params) {
            if (imageView != null && bitmap != null) {
                String url = (String) params[0];
                if (url != null && url.equals((String) imageView.getTag())) {
                    ((ImageView) imageView).setImageBitmap(bitmap);
                } else {
                    Log.e(TAG, "callback, bmp not match");
                }
            } else {
                Log.e(TAG, "callback, bmp null");
            }
        }
    };

    /**
     * 存放列表项控件句柄
     */
    private class ViewHolder {
        public ImageView imageView;
//        public RelativeLayout mRelaChoose;
        public CheckBox mCheckBox;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_camera_img_sel, parent, false);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.id_choose_img);
            viewHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.id_check_pic);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ImageItem item = dataList.get(position);
        String path;
        if (dataList != null && dataList.size() > position)
            path = item.imagePath;
        else
            path = "camera_default";
        if (path.contains("camera_default")) {
            viewHolder.imageView.setImageResource(R.drawable.ic_pl_pic_bg);
        } else {
            viewHolder.imageView.setTag(item.imagePath);
            cache.displayBmp(viewHolder.imageView, item.thumbnailPath, item.imagePath, callback);
        }
        viewHolder.mCheckBox.setTag(position);
        viewHolder.mCheckBox.setOnClickListener(new ToggleClickListener(viewHolder.mCheckBox));
        if (selectedDataList.contains(item)) {
            viewHolder.mCheckBox.setChecked(true);
            viewHolder.mCheckBox.setBackgroundResource(R.drawable.ic_agree_oil_ask);
        } else {
            viewHolder.mCheckBox.setChecked(false);
            viewHolder.mCheckBox.setBackgroundResource(R.drawable.ic_agree_oil_ask_off);
        }
        return convertView;
    }

    private class ToggleClickListener implements OnClickListener {
        CheckBox chooseBt;

        public ToggleClickListener(CheckBox choosebt) {
            this.chooseBt = choosebt;
        }

        @Override
        public void onClick(View view) {
            if (view instanceof CheckBox) {
                CheckBox toggleButton = (CheckBox) view;
                int position = (Integer) toggleButton.getTag();
                if (dataList != null && mOnItemClickListener != null && position < dataList.size()) {
                    mOnItemClickListener.onItemClick(toggleButton, position, toggleButton.isChecked());
                }
            }
        }
    }


    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener l) {
        mOnItemClickListener = l;
    }

    public interface OnItemClickListener {
        public void onItemClick(CheckBox view, int position, boolean isChecked);
    }

}
